/*
 * 文件名：RSAUtils.java
 * 版权：Copyright Copyright by will_awoke
 * 描述：
 * 修改人：will_awoke
 * 修改时间：2014-8-7
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.qbgs.security;


/**
 * RSAUtils
 * @author will_awoke
 * @version 2014-8-7
 * @see RSAUtils
 * @since
 */
public class RSAUtils
{

    private final static byte[] hex = "0123456789ABCDEF".getBytes();

    /**
     * 
     * Description: 字节转换到字符
     * 
     * @param b
     * @return char
     * @see
     */
    public static char byte2char(byte b)
    {
        return (char)b;
    }

    private static int parse(char c)
    {
        if (c >= 'a') return (c - 'a' + 10) & 0x0f;
        if (c >= 'A') return (c - 'A' + 10) & 0x0f;
        return (c - '0') & 0x0f;
    }

    /**
     * 
     * Description: 字节数组到十六进制字符串转换
     * 
     * @param b
     * @return String
     * @see
     */
    public static String Bytes2HexString(byte[] b)
    {
        byte[] buff = new byte[2 * b.length];
        for (int i = 0; i < b.length; i++ )
        {
            buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
            buff[2 * i + 1] = hex[b[i] & 0x0f];
        }
        return new String(buff);
    }

    /**
     * 
     * Description: 从十六进制字符串到字节数组转换
     * 
     * @param hexstr
     * @return byte[]
     * @see
     */
    public static byte[] HexString2Bytes(String hexstr)
    {
        byte[] b = new byte[hexstr.length() / 2];
        int j = 0;
        for (int i = 0; i < b.length; i++ )
        {
            char c0 = hexstr.charAt(j++ );
            char c1 = hexstr.charAt(j++ );
            b[i] = (byte)((parse(c0) << 4) | parse(c1));
        }
        return b;
    }

}
